package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.yayan.xh.bean.BannerBean;

public class BannerDao extends ConnectionBase implements IBannerDao {
	
	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;
	@Override
	public List<BannerBean> getBanner(Map<String, String> map) throws Exception {
		List<BannerBean> result = null;
		try {
			qRunner = new QueryRunner();
			//获取赛事信息
			String sql = "select * from tb_banner where shop_id=?";
			conn = getConn();
			result = qRunner.query(conn, sql, new BeanListHandler<BannerBean>(BannerBean.class), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public BannerBean getBannerById(Map<String, String> map) throws Exception {
		BannerBean result = null;
		try {
			qRunner = new QueryRunner();
			//获取赛事信息
			String sql = "select * from tb_banner where shop_id=? and b_id=?";
			conn = getConn();
			result = qRunner.query(conn, sql, new BeanHandler<BannerBean>(BannerBean.class), map.get("shopId"), map.get("bId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public boolean saveBanner(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_banner(shop_id, b_type, b_link, b_linkname, b_img) values(?,?,?,?,?)";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("type"));
			ps.setObject(3, map.get("link"));
			ps.setObject(4, map.get("linkName"));
			ps.setObject(5, map.get("img"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean delBanner(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "delete from tb_banner where shop_id=? and b_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("bId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean modifyBanner(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_banner set b_type=?, b_link=?, b_linkname=?, b_img=? where shop_id=? and b_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("type"));
			ps.setObject(2, map.get("link"));
			ps.setObject(3, map.get("linkName"));
			ps.setObject(4, map.get("img"));
			ps.setObject(5, map.get("shopId"));
			ps.setObject(6, map.get("bId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public List<Map<String, Object>> getBannerList(Map<String, String> map) throws Exception {
		List<Map<String, Object>> result = null;
		try {
			qRunner = new QueryRunner();
			//获取赛事信息
			String sql = "select b_type as btype, b_link as link, b_img as img from tb_banner where shop_id=?";
			conn = getConn();
			result = qRunner.query(conn, sql, new MapListHandler(), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

}
